//
//  MallAllCommentViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/9/4.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "MallAllCommentViewController.h"
#import "MallCommentTableViewCell.h"
#import "GoodsComment.h"

@interface MallAllCommentViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *commentArr;
@property (nonatomic, assign) NSInteger page; // 网络请求页数

@end

@implementation MallAllCommentViewController{
    UITableView     *_tableView;
    MallCommentTableViewCell    *_cell;
}

- (void)viewWillAppear:(BOOL)animated {
    [self GETComments];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.page = 1;
    
    [self createUI];
    
    [self refreshData];
    
}

#pragma mark ==========构建UI==========
- (void)createUI {
    
    self.navigationItem.title = @"商品评论";
    
    self.view.backgroundColor = kWhiteColor;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"MallCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"mall"];
}

#pragma mark ==========更新数据==========
- (void)refreshData {
    __weak typeof (self) weakSelf = self;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉刷新");
        weakSelf.page = 1;
        [weakSelf GETComments];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"上拉加载");
        weakSelf.page++;
        [weakSelf GETComments];
    }];
    
    
}


#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _cell = [tableView dequeueReusableCellWithIdentifier:@"mall"];
    
    GoodsComment *model = self.commentArr[indexPath.row];
    _cell.model = model;
    
    return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"0-----height = %f", [_cell cellHeight]);
    
    return [_cell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
}


#pragma mark ==========响应方法==========
- (void)GETComments {
    
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%ld", self.page], @"per_page":@"10"};
    NSString *url = [NSString stringWithFormat:@"%@/%ld/comments", APIMallsGoods, (long)self.goodId];
    
    [[NetworkTool sharedTool] requestWithName:@"获取商品评论列表" URLString:url parameters:param method:@"GET" callBack:^(id responseObject) {
        NSArray *arr = responseObject[@"data"];
        
        if (self.page == 1) {
            [self.commentArr removeAllObjects];
        }
        
        if (arr.count == 0) {
            self.page--;
            if (self.page == 0) {
                self.page = 1;
            }
        }
        
        for (NSDictionary *dict in arr) {
            GoodsComment *comment = [[GoodsComment alloc] init];
            [comment setValuesForKeysWithDictionary:dict];
            [self.commentArr addObject:comment];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
            [_tableView.mj_footer endRefreshing];
            [_tableView.mj_header endRefreshing];
            
            
            if (self.page == 1) {
                NSLog(@"我是 %ld", self.page);
                double delayInSeconds = 0.5;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    //执行事件
                    [_tableView reloadData];
                });
            }
            
        });
    }];
}


// 懒加载
- (NSMutableArray *)commentArr {
    if (_commentArr == nil) {
        _commentArr = [[NSMutableArray alloc] init];
    }
    return _commentArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
